﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;

namespace WaiGaoQiaoWeb.Controllers
{
    public class WarehouseController : Controller
    {
        private WarehouseInfo wareinfo = new WarehouseInfo(true);
        public ActionResult WarehouseIndex()
        {
            return View();
        }

        public ActionResult AddWarehouse()
        {
            return View();
        }

        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult EditWarehouse(string id)
        {
            WarehouseVM vm = new WarehouseVM();
            WarehouseInfo info = new WarehouseInfo();
            wareinfo = info.GetWarehouseInfoOne(id);
            vm.ID = wareinfo.ID;
            vm.Warehouse_Name = wareinfo.Warehouse_Name;
            vm.Manager = wareinfo.Manager;
            vm.Remark = wareinfo.Remark;
            return View(vm);
        }

        /// <summary>
        /// 获取仓库列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetWarehouseList(PageVM pvm)
        {
            string query = string.Empty;
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                string[] param = pvm.query.Split(',');
                query = " and Warehouse_Name like '%" + param[0] + "%' and Manager like '%" + param[1] + "%'";
            }
            IList<WarehouseInfo> warelist = wareinfo.SelWarehouseInfo(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = wareinfo._Warehousecount;
            List<string> v;
            foreach (WarehouseInfo item in warelist)
            {
                #region
                v = new List<string>();
                v.Add(item.ID.ToString());
                v.Add(item.Warehouse_Name);
                v.Add(item.Manager);
                v.Add(item.Remark);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.ID.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 保存仓库（新增界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveWarehouse(WarehouseVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                WarehouseInfo ware = new WarehouseInfo()
                {
                    Warehouse_Name = model.Warehouse_Name,
                    Manager = model.Manager,
                    Remark = model.Remark
                };
                result = wareinfo.SaveWarehouse(ware);
            }
            return Json(result);
        }

        /// <summary>
        /// 保存仓库（修改界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveWarehouseEdit(WarehouseVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                WarehouseInfo ware = new WarehouseInfo()
                {
                    ID = model.ID,
                    Warehouse_Name = model.Warehouse_Name,
                    Manager = model.Manager,
                    Remark = model.Remark
                };
                result = wareinfo.SaveWarehouseEdit(ware);
            }
            return Json(result);
        }

        /// <summary>
        /// 删除仓库
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult DeleteWarehouse(string ID)
        {
            string strId = ID.Remove(0, 1);
            int result = 0;
            result = wareinfo.DeleteWarehouse(strId);
            return Json(result);
        }
    }
}
